package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.irremote.lib.net.IRextReqManager;
import com.stark.irremote.lib.net.bean.IrBrand;
import com.stark.irremote.lib.net.bean.IrCategory;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.BrandAdapter;
import flc.ast.adapter.LetterAdapter;
import flc.ast.databinding.ActivitySelBrandBinding;
import flc.ast.utils.Content;
import stark.common.basic.retrofit.IReqRetCallback;

public class SelBrandActivity extends BaseAc<ActivitySelBrandBinding> {

    public static IrCategory bean;
    private LetterAdapter letterAdapter;
    private List<String> listLetter = new ArrayList<>();
    private BrandAdapter brandAdapter;
    private List<IrBrand> listBrand = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_brand;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelBrandBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //品牌
        mDataBinding.rvBrandList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        brandAdapter = new BrandAdapter();
        mDataBinding.rvBrandList.setAdapter(brandAdapter);
        brandAdapter.setOnItemClickListener(this);
        //字母
        mDataBinding.rvLetterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        letterAdapter = new LetterAdapter();
        mDataBinding.rvLetterList.setAdapter(letterAdapter);
        letterAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == letterAdapter) {
            searchLetter(listLetter.get(position));
        } else if (adapter == brandAdapter) {
            gotoRemote(brandAdapter.getItem(position));
        }
    }

    @Override
    protected void initData() {
        IRextReqManager.irextApi().listBrands(this, bean.id, new IReqRetCallback<List<IrBrand>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<IrBrand> result) {
                if (success) {
                    if (result == null) {
                        ToastUtils.showShort(msg);
                        return;
                    }
                    listBrand.addAll(result);
                    brandAdapter.setList(listBrand);
                }
            }
        });
        //获取字母导航数据
        for (String letter : Content.Letter) {
            listLetter.add(letter);
        }
        letterAdapter.setList(listLetter);
    }

    //去到对应遥控器界面
    private void gotoRemote(IrBrand beans) {
        //(注:)机顶盒不是选择品牌而是选择地理位置
        if (getString(R.string.air_conditioner).equals(bean.name)) {
            RemoteKtActivity.isAdd = true;
            RemoteKtActivity.bean = beans;
            startActivity(RemoteKtActivity.class);
        } else if (getString(R.string.television).equals(bean.name)) {
            RemoteDsActivity.isAdd = true;
            RemoteDsActivity.bean = beans;
            startActivity(RemoteDsActivity.class);
        } else if (getString(R.string.network_box).equals(bean.name)) {
            RemoteWlhzActivity.isAdd = true;
            RemoteWlhzActivity.bean = beans;
            startActivity(RemoteWlhzActivity.class);
        } else if (getString(R.string.iptv).equals(bean.name)) {
            RemoteIPTVActivity.isAdd = true;
            RemoteIPTVActivity.bean = beans;
            startActivity(RemoteIPTVActivity.class);
        } else if (getString(R.string.dvd).equals(bean.name)) {
            RemoteDvdActivity.isAdd = true;
            RemoteDvdActivity.bean = beans;
            startActivity(RemoteDvdActivity.class);
        } else if (getString(R.string.fan).equals(bean.name)) {
            RemoteFsActivity.isAdd = true;
            RemoteFsActivity.bean = beans;
            startActivity(RemoteFsActivity.class);
        } else if (getString(R.string.projector).equals(bean.name)) {
            RemoteTyyActivity.isAdd = true;
            RemoteTyyActivity.bean = beans;
            startActivity(RemoteTyyActivity.class);
        } else if (getString(R.string.sound).equals(bean.name)) {
            RemoteYxActivity.isAdd = true;
            RemoteYxActivity.bean = beans;
            startActivity(RemoteYxActivity.class);
        } else if (getString(R.string.lamp).equals(bean.name)) {
            RemoteDActivity.isAdd = true;
            RemoteDActivity.bean = beans;
            startActivity(RemoteDActivity.class);
        } else if (getString(R.string.sweeping_robot).equals(bean.name)) {
            RemoteJqrActivity.isAdd = true;
            RemoteJqrActivity.bean = beans;
            startActivity(RemoteJqrActivity.class);
        } else if (getString(R.string.air_cleaner).equals(bean.name)) {
            RemoteJhqActivity.isAdd = true;
            RemoteJhqActivity.bean = beans;
            startActivity(RemoteJhqActivity.class);
        } else if (getString(R.string.dyson).equals(bean.name)) {
            RemoteDysonActivity.isAdd = true;
            RemoteDysonActivity.bean = beans;
            startActivity(RemoteDysonActivity.class);
        }
    }

    //根据字母搜索品牌
    private void searchLetter(String letter) {
        for (int i = 0; i < listBrand.size(); i++) {
            if (letter.contains(listBrand.get(i).getPinYinFirstChar())) {
                mDataBinding.rvBrandList.getLayoutManager().scrollToPosition(i + 5);
                break;
            }
        }
    }

}